  
$(document).ready(function() {
 
	$('#htxtTargetURL').val(getQueryStringParams("targeturl"));

 
 window.fbAsyncInit = function () {
	 var appid = $('#appId').val();
     FB.init({
         appId: appid, // App ID
         channelUrl: 'localhost', // Channel File
         status: true, // check login status
         cookie: true, // enable cookies to allow the server to access the session
         xfbml: true, // parse XFBML
         frictionlessRequests : true
     });
 };
 // Load the SDK Asynchronously
 (function (d) {
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {
         return;
     }
     js = d.createElement('script');
     js.id = id;
     js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
 } (document));

 //$("#imgFBLogin").click(function(){alert('12');});
// $("#imgFBLogin").click(function () {
	 function loggedin() {
		    FB.getLoginStatus(function (response) {
		    	alert(response.status );
		        if (response.status === 'connected') {
		            FB.api('/me', function (user) {
		                if (!($("#fbid").val()) && ($("#UserName").val())) {
		                    alert('Authentication Failed!');
		                }
		                else {
		                    $("#fbid").val(user.id);
		                    $("#firstName").val(user.first_name);
		                    $("#lastName").val(user.last_name);
		                    $("#userName").val(user.email);
		                    $("#emailAddress").val(user.email);
		                    //SA:Store the user informaion into User entity in server side.
		                    var tempdata = {};
		           		 	tempdata['address'] = '0';
		        			tempdata['age'] = '0';
		        			tempdata['city'] = '0';
		        			tempdata['country'] = '0';
		        			tempdata['dateCreated'] = '20130924';
		        			tempdata['email'] = user.email;
		        			tempdata['facebookId'] = user.id;
		        			tempdata['firstName'] = user.first_name;
		        			tempdata['gender'] = '0';
		        			tempdata['id'] = user.id;
		        			tempdata['lastName'] = user.last_name;
		        			tempdata['memrchantId'] = '1';
		                    addUser(tempdata); //this method source file is server.js
		                    getFriends();
		                }
		            });
		        }
		    });
		}
});



function getQueryStringParams(sParam) {
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            return sParameterName[1];
        }
    }
}




